# Demonstrate how to register services
#In this case it is a simple value service.
angular
    .module("Ajani.services", ['ngResource'])
    .value("version", "0.2")
    .factory("Ping", ["$http", "$timeout", ($http, $timeout) ->
        msg: (cb) ->
            dofn = () ->
                $http
                    .post('/_/ping?q=ibr')
                    .success (data, status, headers, config) ->
                        if data.status
                            if data.status != "error"
                                cb(data)
                        $timeout dofn, 5000
                    .error (data, status, headers, config) ->
                        $timeout dofn, 5000
            dofn()
    ])
    .factory("Items", ['$resource', ($resource) ->
        $resource('/_/items/:res', {}, {
            all:
                method: 'GET'
                params: res: 'list'
            newest:
                method: 'GET'
                params: res: 'newest'
                isArray: true
            pops:
                method: 'GET'
                params: res: 'popular'
                isArray: true
            cat:
                method: 'GET'
                params: res: 'category'
        })
    ])
    .factory("User", ($resource) ->
        $resource('/_/users/:who', {who: "me"}, {
            me:
                method: 'GET'
            update:
                method: 'PUT'
        })
    )
    .factory("Message", ['$resource', ($resource) ->
        $resource('/_/message/:met', {met: 'list'}, {
            list:
                method: 'GET'
            read:
                method: 'PUT'
                params: met: 'read'
        })
    ])
    .factory("Admin",['$resource',  ($resource) ->
        $resource('/_/admin/:res/:met', {}, {
            users:
                method: 'GET'
                params:
                    res: 'users'
                    met: 'list'
            items:
                method: 'GET'
                params:
                    res: 'items'
                    met: 'list'
        })
    ])